//
//  CustomAlertView.m
//  DepressionAuxiliary
//
//  Created by ZhenYu Wang on 2021/5/13.
//

#import "CustomAlertView.h"
#import "AppDelegate.h"

@interface CustomAlertView() 

@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIView *showView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UILabel *loadingLabel;

@end

@implementation CustomAlertView

- (id)initItem:(NSString *)contentStr withButtons:(NSArray *)btns{
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomAlertView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        float width = 260 * SCREEN_SCALE_320;
        [self.showView setFrame:CGRectMake((SCREEN_WIDTH_DEVICE-width)/2.0, (SCREEN_HEIGHT_DEVICE-244)/2.0, width, 244)];
        self.showView.layer.masksToBounds = YES;
        self.showView.layer.cornerRadius = 20.0f;
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:contentStr];
        [text addAttribute:NSForegroundColorAttributeName value:TextColor range:NSMakeRange(0,text.length)];
        [text addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] range:NSMakeRange(0,text.length)];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 10;// 字体的行间距
        paragraphStyle.alignment = NSTextAlignmentCenter;
        [text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
        self.contentLabel.attributedText = text;
        
        if (btns.count == 1) {
            self.leftBtn.hidden = YES;
            [self.rightBtn setFrame:CGRectMake((width-108)/2.0, 164, 108, 40)];
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.rightBtn setTitle:[btns firstObject] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundColor:ThemeColor];
        } else {
            float widthButton = (width - 80 - 20)/2.0;
            [self.leftBtn setFrame:CGRectMake(40, 164, widthButton, 40)];
            [self.rightBtn setFrame:CGRectMake(40 + widthButton + 20, 164, widthButton, 40)];
            self.leftBtn.layer.masksToBounds = YES;
            self.leftBtn.layer.cornerRadius = 16.0f;
            self.leftBtn.layer.borderWidth = 1.0f;
            self.leftBtn.layer.borderColor = ThemeColor.CGColor;
            [self.leftBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
            self.rightBtn.layer.masksToBounds = YES;
            self.rightBtn.layer.cornerRadius = 20.0f;
            self.leftBtn.layer.masksToBounds = YES;
            self.leftBtn.layer.cornerRadius = 20.0f;
            [self.leftBtn setTitle:[btns firstObject] forState:UIControlStateNormal];

            [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.rightBtn setTitle:[btns objectAtIndex:1] forState:UIControlStateNormal];
            [self.rightBtn setBackgroundColor:ThemeColor];
        }
        
        
        if (SCREEN_SCALE_320 == 1) {
            self.leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
            self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
        }
    }
    return self;
}

- (void)show {
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [del.window addSubview:self];
    self.showView.layer.opacity = 0.5f;
    self.showView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
                         self.showView.layer.opacity = 1.0f;
                         self.showView.layer.transform = CATransform3DMakeScale(1, 1, 1);
                     }
                     completion:NULL
     ];
}

- (IBAction)leftPress {
    if (self.CancelHandle) {
        self.CancelHandle();
    }
    [self removePress];
}

- (IBAction)rightPress {
    if ([self.rightBtn.titleLabel.text isEqualToString:@"取消"]) {
        if (self.CancelRequestHandle) {
            self.CancelRequestHandle();
        }
    } else {
        if (self.SureHandle) {
            self.SureHandle();
        }
    }
    
    [self removePress];
}

- (IBAction)removePress {
    [self removeFromSuperview];
}

- (void)backgroundClickDisable {
    self.removeBtn.userInteractionEnabled = NO;
}

- (void)backgroundClickEnable {
    self.removeBtn.userInteractionEnabled = YES;
}
@end
